package cn.sxt.bus.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.bus.domain.Goods;
import cn.sxt.bus.domain.Provider;
import cn.sxt.bus.service.GoodsService;
import cn.sxt.bus.service.ProviderService;
import cn.sxt.bus.vo.GoodsVo;
import cn.sxt.bus.vo.ProviderVo;
import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.utils.TreeNode;

@Controller
@RequestMapping("goods")
public class GoodsController {

	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private ProviderService providerService;

	// 跳到goodsManager.jsp
	@RequestMapping("toGoodsManager")
	public String toGoodsManager() {
		return "business/goods/goodsManager";
	}

	// goodsManager.jsp加载 左边
	@RequestMapping("toGoodsLeft")
	public String toGoodsLeft() {
		return "business/goods/goodsLeft";
	}

	// goodsManager.jsp加载 右边
	@RequestMapping("toGoodsRight")
	public String toGoodsRight() {
		return "business/goods/goodsRight";
	}

	// 加载左边 供应商树
	@RequestMapping("loadProviderTree")
	@ResponseBody
	public List<TreeNode> loadProviderTree(ProviderVo providerVo) {
		List<TreeNode> treeNodes = new ArrayList

				<>();
		List<Provider> providers = providerService.queryAllproviderServiceForList(providerVo);
		//Goods goodsz=new Goods();  //自建父节点
		treeNodes.add(new TreeNode(0, null, "所有供应商", true, true));//自建父节点
		for (Provider p : providers) {
			Boolean open = false;
			Boolean isParent = false;
			
			treeNodes.add(new TreeNode(p.getId(), 0, p.getProvidername(), open, isParent));
		}
		return treeNodes;
	}

	// 部门数据，分页，模糊
	@RequestMapping("queryAllGoods")
	@ResponseBody
	public DataGridView queryAllGoods(GoodsVo goodsVo) {
		return goodsService.queryAllGoods(goodsVo);
	}

	// 跳添加页面
	@RequestMapping("toAddGoods")
	public String toAddGoods() {
		return "business/goods/goodsAdd";
	}

	// 添加
	@RequestMapping("addGoods")
	@ResponseBody
	public Map<String, Object> addGoods(GoodsVo goodsVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = goodsService.addGoods(goodsVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateGoods")
	public String toUpdateGoods(GoodsVo goodsVo, Model model) {
		Goods goods = goodsService.queryGoodsById(goodsVo.getId());
		model.addAttribute("goods", goods);
		return "business/goods/goodsUpdate";
	}

	// 修改
	@RequestMapping("updateGoods")
	@ResponseBody
	public Map<String, Object> updateGoods(GoodsVo goodsVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = goodsService.updateGoods(goodsVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deleteGoods")
	@ResponseBody
	public Map<String, Object> deleteGoods(GoodsVo goodsVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = goodsService.deleteGoodsById(goodsVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除商品
	@RequestMapping("batchDeleteGoods")
	@ResponseBody
	public Map<String, Object> batchDeleteGoods(GoodsVo goodsVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = goodsVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					goodsVo.setId(ids[i]);
					int j = goodsService.deleteGoodsById(goodsVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	//根据供应商id查供应商下面的商品
	@RequestMapping("loadGoodsByProviderId")
	@ResponseBody
	public List<GoodsVo> loadGoodsByProviderId(GoodsVo goodsVo){
		return goodsService.queryAllGoodsForList(goodsVo);
	}
}
